package com.yanqu.road.server.gameplayer.module.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.player.PlayerPotentialDaoImpl;
import com.yanqu.road.dao.impl.player.UserPotentialDaoImpl;
import com.yanqu.road.entity.player.PlatformUserPotential;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.player.UserPotentialData;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.logic.redis.RedisUserServerList;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.utils.date.DateHelper;

public class PotentialModule  extends GeneralModule {

    private UserPotentialData potentialData;


    public PotentialModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        potentialData = new UserPotentialDaoImpl().getUserPotentialData(player.getUserId());
        if (potentialData == null) {
            potentialData = new UserPotentialData();
            potentialData.setUserId(player.getUserId());
            potentialData.setPotential(0);
            potentialData.setLastLoadTime(0);
            potentialData.setInsertOption();
        }
        if (potentialData.getPotential() == 0) {
            loadFromServerDb();
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    private void loadFromServerDb() {

        if (potentialData == null) {
            return;
        }

        if (potentialData.getPotential() > 0) {
            return;
        }

        boolean needLoad = false;
        UserInfo userInfo = player.getUserInfo();
        long registerTime = userInfo.getRegisterTime();
        if (potentialData.getLastLoadTime() / 1000 > registerTime + 24 * 3600) {
            return;
        }
        if (System.currentTimeMillis() - potentialData.getLastLoadTime() < 60 * 1000) {
            return;
        }
        if (potentialData.getLastLoadTime() == 0) {
            // 没加载过 加载一下吧
            needLoad = true;
        }else {

            if (System.currentTimeMillis() / 1000 - registerTime < 3600) {
                // 小于1小时
                needLoad = true;
            } else if (DateHelper.getZeroTimeStamp(System.currentTimeMillis()) > DateHelper.getZeroTimeStamp(registerTime * 1000)) {
                //不是同一天
                needLoad = true;
            }
        }

        if (needLoad) {
           /* potentialData.setLastLoadTime(System.currentTimeMillis());
            PlatformUserPotential platformUserPotential = new PlayerPotentialDaoImpl().getPlayerPotential(userInfo.getPlatformUserId());
            if (platformUserPotential != null) {
                long potential = platformUserPotential.getPotential();
                potentialData.setPotential((int)potential);
            }*/
            try {
                String jsonStr = RedisManger.getRedisUtil().get("POTENTIAL-" + userInfo.getPlatformUserId());
                if (jsonStr != null) {
                    JSONObject object = JSON.parseObject(jsonStr);
                    if (object != null) {
                        long potential = object.getLong("potential");
                        potentialData.setPotential((int)potential);
                    }
                }
            } catch (Exception e) {
                log.error("read potential from redis error. userId: {}",getUserId(),e);
            }

        }
    }

    @Override
    public boolean saveData() {
        if (potentialData != null) {
            if (potentialData.isInsertOption()) {
                new UserPotentialDaoImpl().add(potentialData);
            } else if (potentialData.isUpdateOption()) {
                new UserPotentialDaoImpl().update(potentialData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        loadFromServerDb();
    }


    public int getPotential() {
        if (potentialData == null) {
            return 0;
        }
        return potentialData.getPotential();
    }

}
